<?php
require_once './vendor/autoload.php';
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
/** 
 * @author Administrator
 * 
 */
class SmsSender {
	
	/**
	 */
	function __construct() {
	}
	/**
	 * 发送短信验证码
	 * @param string $phone
	 * @param string $code
	 */
	public function captcha($phone,$code){
		try{
		 	AlibabaCloud::accessKeyClient('LTAI5tRpA13MUrFVmbvthSMs','5MD7KNXIBIEO10NDUxncqPVYJSdfb8')->regionId('cn-hangzhou')->asDefaultClient();
			$params = array('code'=>$code);
			$result = AlibabaCloud::dysmsapi()->v20170525()->sendSms();
			$result = $result->withPhoneNumbers($phone)  //发送手机号
                      ->withSignName('荣钧健康饮水')  //设置签名
                      ->withTemplateCode('SMS_223587547')  //设置模板
                      ->withTemplateParam(json_encode($params,JSON_UNESCAPED_UNICODE))  //请求参数
                      ->request();
            print_r($result->toArray());
        }catch (ClientException $e) {
		    // Print the error code
		    echo $e->getErrorCode() . PHP_EOL;
		    // Print the error message
		    echo $e->getErrorMessage() . PHP_EOL;
		} catch (ServerException $e) {
		    // Print the error code
		    echo $e->getErrorCode() . PHP_EOL;
		    // Print the error message
		    echo $e->getErrorMessage() . PHP_EOL;
		    // Print the RequestId
		    echo $e->getRequestId() . PHP_EOL;
		    // Convert the result to an array and print
		    print_r($e->getResult()->toArray());
		}
	}
	
	/**
	 * 发送短信验证码
	 * @param string $phone
	 * @param string $code
	 */
	public function captcha_bao($phone,$code){
		$statusStr = array(
			"0" => "短信发送成功",
			"-1" => "参数不全",
			"-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
			"30" => "密码错误",
			"40" => "账号不存在",
			"41" => "余额不足",
			"42" => "帐户已过期",
			"43" => "IP地址限制",
			"50" => "内容含有敏感词"
		);
		$smsapi = "http://api.smsbao.com/";
		$user = "runjoy888"; //短信平台帐号
		$pass = "10e6268eeec1432ab1cf64ac7ede1f5e"; //短信平台密码
		$time = "十";
		$sign_name = '荣钧健康饮水';
		$content="【{$sign_name}】您的验证码是:{$code}，{$time}分钟内有效。如非本人操作，请忽略本条短信。";//要发送的短信内容
		$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
		try{
			$result =file_get_contents($sendurl);
			$status_txt = $statusStr[$result];
			if($result == 0){
				return true;
			}else{
				return false;
			}
		}catch(\Exception $e){

			return false;
		}
	}
}

$obj = new SmsSender();
$obj->captcha('18975653252','123456');